package cn.uppower.wordaliy.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.litepal.LitePal;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.uppower.wordaliy.R;
import cn.uppower.wordaliy.WordaliyApplication;
import cn.uppower.wordaliy.activity.CnEngChoiceActivity;
import cn.uppower.wordaliy.activity.FavoriteWordsActivity;
import cn.uppower.wordaliy.activity.TestActivity;
import cn.uppower.wordaliy.model.FavoriteWords;
import cn.uppower.wordaliy.model.PageWord;
import cn.uppower.wordaliy.model.Words;
import cn.uppower.wordaliy.model.WordsRvItem;
import cn.uppower.wordaliy.util.WordsUtils;

/**
 * Created by elitetyc on 2018/11/26.
 * Describe:
 */
public class WordsFragment extends Fragment {

    private RecyclerView rv_words;
    private List<WordsRvItem> wordsRvItems;
    private Context mContext;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view= View.inflate(getActivity(), R.layout.fragment_words,null);
        mContext=getActivity();
        rv_words = view.findViewById(R.id.rv_words);
        initData();
        rv_words.setLayoutManager(new GridLayoutManager(getContext(),2));
        rv_words.setAdapter(new WordsItemAdapter());
        return view;
    }

    private void initData() {
        wordsRvItems=new ArrayList<>();
        wordsRvItems.add(new WordsRvItem(R.drawable.ywxy_green,"英文选意"));
        wordsRvItems.add(new WordsRvItem(R.drawable.zwxc_green,"中文选词"));
        wordsRvItems.add(new WordsRvItem(R.drawable.pxtk_green,"生词本"));
        wordsRvItems.add(new WordsRvItem(R.drawable.pxzh_green,"学习测试"));
        wordsRvItems.add(new WordsRvItem(R.drawable.qplx_green,"全拼练习"));
        wordsRvItems.add(new WordsRvItem(R.drawable.tyby_green,"听音辨意"));
        wordsRvItems.add(new WordsRvItem(R.drawable.tysj_green,"听音速记"));

    }


    class WordsItemAdapter extends RecyclerView.Adapter<WordsItemAdapter.WordsItemHolder>{


        @NonNull
        @Override
        public WordsItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view= LayoutInflater.from(mContext).inflate(R.layout.item_words_item, parent, false);
            return new WordsItemHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull WordsItemHolder holder, int position) {
            WordsRvItem wordsRvItem=wordsRvItems.get(position);
            holder.iv_image.setImageResource(wordsRvItem.getResId());
            holder.tv_desc.setText(wordsRvItem.getDesc());
            holder.rl_words.setOnClickListener((e)->{
                switch (position){
                    case 0:
                        Intent intent =new Intent(mContext,CnEngChoiceActivity.class);
                        intent.putExtra("words", (Serializable) WordsUtils.getYwxyWords(null));
                        intent.putExtra("type",1);
                        startActivity(intent);
                        break;
                    case 1:
                        Intent intent1 =new Intent(mContext,CnEngChoiceActivity.class);
                        intent1.putExtra("words", (Serializable) WordsUtils.getZwxcWords());
                        intent1.putExtra("type",2);
                        startActivity(intent1);
                        break;
                    case 2:
                        List<FavoriteWords> favoriteWords=LitePal.where("userPhone = ?",WordaliyApplication.userInfo.getUserPhone()).find(FavoriteWords.class);
                        List<Words> wordsList =new ArrayList<>();
                        for (FavoriteWords favoriteWords1:favoriteWords){
                            wordsList.add(LitePal.where("id = ?",String.valueOf(favoriteWords1.getWordsId())).findFirst(Words.class));
                        }
                        Intent intent2 =new Intent(mContext,FavoriteWordsActivity.class);
                        intent2.putExtra("favWords", (Serializable) wordsList);
                        startActivity(intent2);
                        break;
                    case 3:
                        startActivity(new Intent(mContext,TestActivity.class));
                        break;
                    case 4:
                        Toast.makeText(mContext, "全拼练习，待开发", Toast.LENGTH_SHORT).show();
                        break;
                    case 5:
                        Toast.makeText(mContext, "听音辨意，待开发", Toast.LENGTH_SHORT).show();
                        break;
                    case 6:
                        Toast.makeText(mContext, "听音速记，待开发", Toast.LENGTH_SHORT).show();
                        break;

                }
            });

        }

        @Override
        public int getItemCount() {
            return wordsRvItems.size();
        }

        class WordsItemHolder extends RecyclerView.ViewHolder {
            private ImageView iv_image;
            private TextView tv_desc;
            private RelativeLayout rl_words;

            public WordsItemHolder(View itemView) {
                super(itemView);
                iv_image = itemView.findViewById(R.id.iv_image);
                tv_desc = itemView.findViewById(R.id.tv_desc);
                rl_words = itemView.findViewById(R.id.rl_words);
            }
        }
    }
}
